package luke4fun.wheresmymoney.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import luke4fun.wheresmymoney.domain.Store;

public class StoreDAO {

	private static StoreDAO instance = null;
	
	private StoreDAO() {
	}
	
	public static StoreDAO getInstance() {
		if (instance == null) {
			instance = new StoreDAO();
		}
		
		return instance;
	}
	
	public List<Store> queryStore(SQLiteDatabase db, String store) {
		List<Store> list = new ArrayList<Store>();
		Cursor cursor = null;
		
		if (store != null && store.length() > 0) {
			cursor = db.query(DBConstants.TABLE_STORE, 
							null, 
							DBConstants.ITEM_NAME + "=?",
							new String[] { store }, 
							null, 
							null, 
							null);
		} else {
			cursor = db.query(DBConstants.TABLE_STORE, 
							null, 
							null, 
							null, 
							null, 
							null, 
							null);
		}
		
		while (cursor.moveToNext()) {
			Store st = new Store();
			st.setId(cursor.getLong(cursor.getColumnIndex(DBConstants.STORE_ID)));
			st.setName(cursor.getString(cursor.getColumnIndex(DBConstants.STORE_NAME)));
			
			list.add(st);
		}
		
		return list;
	}
	
	public long insertStore(SQLiteDatabase db, String store) {
		ContentValues cv = new ContentValues();
		cv.put(DBConstants.ITEM_NAME, store);
		return db.insert(DBConstants.TABLE_STORE, null, cv);
	}
}
